<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '信息'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" style="display: flex;
    flex-flow: wrap;" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题" >
      <div class="el_div">{{dataForm.title}}</div>
    </el-form-item>
      <el-form-item label="内容" >
        <div class="el_div">{{dataForm.content}}</div>
      </el-form-item>
    <el-form-item label="任务类型" >
      <div class="el_div" v-if="dataForm.type==1">监控中心任务</div>
      <div class="el_div" v-if="dataForm.type==2">社会单位任务</div>

    </el-form-item>
    <el-form-item label="接收单位"  >
      <div class="el_div" >{{dataForm.receiveUnitName}}</div>
    </el-form-item>
    <el-form-item label="任务时间" >
      <div class="el_div">{{dataForm.beginTime}} - {{dataForm.endTime}}</div>

    </el-form-item>

      <el-form-item label="任务文件">
        <div class="el_div" style="cursor:pointer" v-if="dataForm.fileName" @click="handleDownload" >{{dataForm.fileName}}</div>
      </el-form-item>

      <el-form-item label="办理人" v-if="dataForm.handleName">
        <div class="el_div">{{dataForm.handleName}}</div>
      </el-form-item>
      <el-form-item label="办理时间" v-if="dataForm.handleTime" >
        <div  class="el_div" >{{dataForm.handleTime}}</div>
      </el-form-item>
      <el-form-item label="办理内容" v-if="dataForm.handleContent" >
        <div class="el_div" >{{dataForm.handleContent}}</div>
      </el-form-item>
      <el-form-item label="办理文件" v-if="dataForm.handleFileName" >
        <div class="el_div" style="cursor:pointer" @click="handleFileDownload">{{dataForm.handleFileName}}</div>
      </el-form-item>
      <el-form-item label="是否超时" v-if="dataForm.isCs">
        <div class="el_div" v-if="dataForm.isCs==0">未超时</div>
        <div class="el_div" v-if="dataForm.isCs==1">已超时</div>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<style lang="scss" scoped>
  .el-form-item{
    width: 50%;
  }
  .el_div{
    color: rgb(204,204,204);
  }
</style>
<script>
  export default {
    data () {
      return {
        visible: false,
        uploadUrl: '',
        beginEndTime:[],
        dataForm: {
          id: 0,
          createTime: '',

          createBy: '',

          updateTime: '',

          updateBy: '',

          isDel: '',

          unitId: '',

          unitName: '',

          title: '',

          content: '',

          fileId: '',

          fileName: '',

          receiveUnitId: '',

          receiveUnitName: '',

          status: '',

          beginTime: '',

          endTime: '',

          handleBy: '',

          handleName: '',

          handleTime: '',

          handleContent: '',

          handleFileId: '',

          handleFileName: '',

          isCs: '',

          type: '1'

        },
        socialUnitList:[],
        receiveUnitIds:[],
        fileList:[],
        content:'',
        tinymcekey:0,
        dataRule: {

          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],

          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          receiveUnitId: [
            { required: true, message: '接收单位不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      dateSel() {
        let obj = this.beginEndTime;
        this.dataForm.beginTime= obj[0];
        this.dataForm.endTime= obj[1]
      },
      pickerOptionsDate1: {
        onPick: (obj) => {
          this.pickerMinDate = new Date(obj.minDate).getTime();
        },
        disabledDate: time => {
          var date = new Date('2019-12-31');
          var time1 = date.getTime();
          return time.getTime() > Date.now() || time.getTime() < time1
        }
      },
      handleDownload() {
        window.open(this.$http.adornUrl('/api-file/file/download/file?fileId=' + this.dataForm.fileId))
      },
      handleFileDownload(){
        window.open(this.$http.adornUrl('/api-file/file/download/file?fileId=' + this.dataForm.handleFileId))
      },
      init (id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          this.beginEndTime=[];
          this.receiveUnitIds=[];
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/api-center/task/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.data.createTime;
                this.dataForm.createBy = data.data.createBy;
                this.dataForm.updateTime = data.data.updateTime;
                this.dataForm.updateBy = data.data.updateBy;
                this.dataForm.isDel = data.data.isDel;
                this.dataForm.unitId = data.data.unitId;
                this.dataForm.unitName = data.data.unitName;
                this.dataForm.title = data.data.title;
                this.dataForm.content = data.data.content;
                this.dataForm.fileId = data.data.fileId;
                this.dataForm.fileName = data.data.fileName;
                this.dataForm.receiveUnitId = data.data.receiveUnitId;
                this.dataForm.receiveUnitName = data.data.receiveUnitName;
                this.dataForm.status = data.data.status;
                this.dataForm.beginTime = data.data.beginTime;
                this.dataForm.endTime = data.data.endTime;
                this.beginEndTime.push(data.data.beginTime);
                this.beginEndTime.push(data.data.beginTime);
                this.dataForm.handleBy = data.data.handleBy;
                this.dataForm.handleName = data.data.handleName;
                this.dataForm.handleTime = data.data.handleTime;
                this.dataForm.handleContent = data.data.handleContent;
                this.dataForm.handleFileId = data.data.handleFileId;
                this.dataForm.handleFileName = data.data.handleFileName;
                this.dataForm.isCs = data.data.isCs;
                this.dataForm.type = data.data.type+'';
                var strs=data.data.receiveUnitId.split(',');
                for (let i = 0; i < strs.length; i++) {
                  this.receiveUnitIds.push(strs[i])
                }
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if(this.dataForm.beginTime ==''){
          this.$message.error("任务时间不能为空");
          return;
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/api-center/task/${!this.dataForm.id ? 'saveBatch' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createTime': this.dataForm.createTime,

                'createBy': this.dataForm.createBy,

                'updateTime': this.dataForm.updateTime,

                'updateBy': this.dataForm.updateBy,

                'isDel': this.dataForm.isDel,

                'unitId': this.dataForm.unitId,

                'unitName': this.dataForm.unitName,

                'title': this.dataForm.title,

                'content': this.dataForm.content,

                'fileId': this.dataForm.fileId,

                'fileName': this.dataForm.fileName,

                'receiveUnitId': this.dataForm.receiveUnitId,

                'receiveUnitName': this.dataForm.receiveUnitName,

                'status': this.dataForm.status,

                'beginTime': this.dataForm.beginTime,

                'endTime': this.dataForm.endTime,

                'handleBy': this.dataForm.handleBy,

                'handleName': this.dataForm.handleName,

                'handleTime': this.dataForm.handleTime,

                'handleContent': this.dataForm.handleContent,

                'handleFileId': this.dataForm.handleFileId,

                'handleFileName': this.dataForm.handleFileName,

                'isCs': this.dataForm.isCs,

                'type': this.dataForm.type

              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      beforeRemove (file, fileList) {
        return new Promise((response,reject)=>{
          this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              console.log(file);
              return response();
            })
            .catch(() => {
              return reject();
            });
        });
      },
      handleRemove (file, fileList) {
        this.deleteFile(file);
      },
      handlePreview (file) {
        window.open(this.$http.adornUrl('/api-file/file/view/file?fileId='+this.dataForm.fileId), '_blank').location;
        /*if(this.dataForm.postCertificate){
          window.open('/file/view/file?fileId='+this.dataForm.postCertificate, '_blank').location
        }else{
          this.$alert('请选择图片并上传到服务器！','重要提示', {
            confirmButtonText: '确定'
          })
        }*/
      },
      beforeUpload (file) {
        this.dataForm.fileName=file.name;
        const isLt100M = file.size / 1024 / 1024 < 100;
        if (!isLt100M) {
          this.$message.error('上传文件大小不能超过 100MB!')
        }
        return isLt100M
      },
      handleSuccess (response, file, fileList) {
        if (response && response.code === 0) {
          this.dataForm.fileId = response.data
        }

      },
      // 文件个数超出
      handleExceed() {
        this.$message.error(`只允许上传单个文件`);
      },
      // 删除文件
      deleteFile (file) {
        let that =this;
        this.$http({
          url: this.$http.adornUrl('/api-file/file/deleteByFileId'),
          method: 'get',
          params: this.$http.adornParams({
            'fileId': this.dataForm.fileId,
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            that.dataForm.fileId='';
            this.$alert('文件已删除！','重要提示', {
              confirmButtonText: '确定'
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 查看文件
      /* lookFile (id) {
         this.$http({
           url: ('/file/view/file?fileId' + id),
           method: 'get',
           params: this.$http.adornParams()
         }).then(({data}) => {
           if (data && data.code === 0) {
             this.fileList = data.data
           }
         })
       },*/
      dateFormCancel() {
        if (this.dataForm.fileId) {
          this.visible = false;
        }else {
          this.visible = false;
        }
      }
    }
  }
</script>
